home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / win / steph1b0.zip / EDITFN.C < prev    next >
C/C++ Source or Header  |  1994-09-27  |  21KB  |  954 lines

  1. /* EDITFN.C */
  2. /* v1.1 11/10/94 */
  3. /* Example code by S Morphet TTDW, 1994 */
  4. /* Editor functions to integrate with Steph software */
  5.  
  6.  
  7. #include "d:\cwork\steph\include\steph.h"
  8. #include "d:\cwork\steph\editor\editfn.h"
  9. #include <stdio.h>
  10. #include <malloc.h>
  11. #include <string.h>
  12.  
  13.  
  14. void editor_initialise( void )
  15. {
  16.    unsigned count;
  17.  
  18.    /* Turn cursors off for all windows, and default to csr at 0,0 */
  19.  
  20.    /* Data is stored for each window. Initialise it. */
  21.  
  22.    for( count = 0; count < __MAXWINDOWS; count++ )
  23.    {
  24.         _editor_spec.info[count].csr_x = 0;
  25.       _editor_spec.info[count].csr_y = 0;
  26.     }
  27.  
  28.     _editor_spec.sel_win = -1;
  29.  
  30. }
  31.  
  32.  
  33. /* this is the keyboard filter for the editor */
  34.  
  35. void editor_filter( void )
  36. {
  37.    /* an editor keyboard filter function */
  38.    char key1 = _window_spec.key1;
  39.    char key2 = _window_spec.key2;
  40.  
  41.    if( _window_spec.key1 == 0 )
  42.    {
  43.       if(   (key2 == 75) || (key2 == 77) ||       /* pass cursors */
  44.             (key2 == 72) || (key2 == 80) ||       /* PGUPDN, HOMEEND etc */
  45.             (key2 == 79) || (key2 == 71) ||
  46.             (key2 == 73) || (key2 == 81) ||
  47.             (key2 == 83) || (key2 == 82) )
  48.       {
  49.          return;
  50.       }
  51.       else
  52.       {
  53.          _window_spec.key2 = 0;
  54.          return;
  55.       }
  56.    }
  57.    else
  58.    {
  59.       if( (key1 >= 32 ) && (key1 <= 126 ) )      /* pass alphanumerics */
  60.          return;
  61.       else
  62.          if( ( key1 == 8 ) || ( key1 == 13 ) )   /* pass bspace and return */
  63.             return;
  64.          else
  65.             _window_spec.key1 = 0;
  66.    }
  67. }
  68.  
  69.  
  70. /* place the cursor on the screen, and set the scroll bars accordingly */
  71. void ed_put_cursor( unsigned win )
  72. {
  73.    int xrange, yrange;   /* character cursor ranges */
  74.  
  75.    _s_settextposition(
  76.          (char)(ewi.csr_y + _window[win].usetop
  77.          - _window[win].lineo ),
  78.          (char)( ewi.csr_x + 2 - _window[win].charo ) );
  79.  
  80.    _s_displaycursor( ON );
  81.  
  82.    xrange = CHARMAX - 78;
  83.  
  84.    yrange = _window[win].blines;
  85.    if( yrange > 0 )
  86.       yrange -= 1;
  87.  
  88.    _window_set_hscroll_pc( win, ( _window[win].charo * 100 / xrange   ) );
  89.  
  90.    if( yrange != 0 )  /* avoid divide by zero */
  91.    {
  92.       _window_set_vscroll_pc( win, ( ewi.csr_y * 100 / yrange   ) );
  93.    }
  94.    else
  95.    {
  96.       _window_set_vscroll_pc( win, 0 );
  97.    }
  98.  
  99.    _window_drawscrollblips();
  100. }
  101.  
  102.  
  103. /* get a line from the window contents buffer */
  104. char *get_line( unsigned win, unsigned line )
  105. {
  106.    if( line >= _window[win].blines )
  107.       return NULL;
  108.  
  109.    _wb_gotoline( win, line );
  110.    return _window[win].pwb->line;
  111. }
  112.  
  113. /* adjust the x cursor to be within the current line */
  114. void fix_x_cursor( void )
  115. {
  116.    char *templine;
  117.    unsigned lenline;
  118.  
  119.    templine = get_line( _window_spec.active, ewi.csr_y );
  120.    lenline = strlen( templine );
  121.  
  122.    while( ewi.csr_x > lenline )
  123.       ewi.csr_x--;
  124. }
  125.  
  126.  
  127. /* adjust the line offset to get the cursor back on screen */
  128. void fix_lineo( void )
  129. {
  130.    unsigned winsize = _window[_window_spec.active].usebottom -
  131.          _window[_window_spec.active].usetop + 1;
  132.  
  133.    while( ewi.csr_y >= (_window[_window_spec.active].lineo + winsize ) )
  134.       _window[_window_spec.active].lineo += 1;
  135.  
  136.    while( ewi.csr_y < _window[_window_spec.active].lineo )
  137.       _window[_window_spec.active].lineo -= 1;
  138. }
  139.  
  140.  
  141. /* adjust the character offset to get the cursor back on screen */
  142. void fix_charo( void )
  143. {
  144.    while( ewi.csr_x >= (_window[_window_spec.active].charo + 78 ) )
  145.       _window[_window_spec.active].charo += 1;
  146.  
  147.    while( ewi.csr_x < _window[_window_spec.active].charo   )
  148.       _window[_window_spec.active].charo -= 1;
  149. }
  150.  
  151.  
  152. /* mouse action handlers */
  153.  
  154. void editor_mouse_select( void )
  155. {
  156.    /* move the cursor to the click position */
  157.  
  158.     while( _mouse_nowbutton() );
  159.  
  160.    if( _window[_window_spec.active].blines != 0 )
  161.    {
  162.       ewi.csr_y = _window_spec.selecty + _window[_window_spec.active].lineo;
  163.  
  164.       while( ewi.csr_y >= _window[_window_spec.active].blines )
  165.          ewi.csr_y--;
  166.  
  167.       ewi.csr_x = _window_spec.selectx + _window[_window_spec.active].charo;
  168.       fix_x_cursor();
  169.    }
  170.  
  171.    /* reposition the cursor */
  172.    ed_put_cursor( _window_spec.active );
  173.  
  174. }
  175.  
  176. void editor_mouse_unselect( void )
  177. {
  178.    _s_displaycursor( OFF );
  179. }
  180.  
  181. void editor_mouse_activate( void )
  182. {
  183.    ed_put_cursor( _window_spec.active );
  184. }
  185.  
  186. void editor_mouse_minimise( void )
  187. {
  188.    fix_lineo();
  189.    _window_refresh( _window_spec.active );
  190.    ed_put_cursor( _window_spec.active );
  191. }
  192.  
  193. void editor_mouse_maximise( void )
  194. {
  195.    ed_put_cursor( _window_spec.active );
  196. }
  197.  
  198. void editor_mouse_resize( void )
  199. {
  200.    fix_lineo();
  201.    _window_refresh( _window_spec.active );
  202.    ed_put_cursor( _window_spec.active );
  203. }
  204.  
  205. /* scroll bar hooks */
  206.  
  207. void ems_left( void )
  208. {
  209.    /* move left by one char */
  210.    if( ewi.csr_x > 0 )
  211.       ewi.csr_x -= SMALL_SCROLL;
  212.  
  213.    fix_charo();
  214.    _window_refresh( _window_spec.active );
  215.    ed_put_cursor( _window_spec.active );
  216. }
  217.  
  218. void ems_right( void )
  219. {
  220.    /* move right by one char */
  221.    char *templine;
  222.    templine = get_line( _window_spec.active, ewi.csr_y );
  223.  
  224.    if( ewi.csr_x < strlen( templine ) )
  225.       ewi.csr_x += SMALL_SCROLL;
  226.  
  227.    fix_charo();
  228.    _window_refresh( _window_spec.active );
  229.    ed_put_cursor( _window_spec.active );
  230. }
  231.  
  232. void ems_pleft( void )
  233. {
  234.    /* move left by a page   */
  235.    if( ewi.csr_x >= BIG_SCROLL )
  236.       ewi.csr_x -= BIG_SCROLL;
  237.    else
  238.       ewi.csr_x = 0;
  239.  
  240.    fix_charo();
  241.    _window_refresh( _window_spec.active );
  242.    ed_put_cursor( _window_spec.active );
  243. }
  244.  
  245. void ems_pright( void )
  246. {
  247.    /* move right by one page */
  248.    char *templine;
  249.    templine = get_line( _window_spec.active, ewi.csr_y );
  250.  
  251.    ewi.csr_x += BIG_SCROLL;
  252.  
  253.     //while( ewi.csr_x > strlen( templine )    )
  254.     //     ewi.csr_x--;
  255.  
  256.     fix_x_cursor();
  257.  
  258.    fix_charo();
  259.    _window_refresh( _window_spec.active );
  260.    ed_put_cursor( _window_spec.active );
  261. }
  262.  
  263. void ems_hdrag( void )
  264. {
  265.    /* set charo to percent of width */
  266.  
  267.    _window[_window_spec.active].charo =
  268.          _window_spec.scrinfo * (CHARMAX - 78 ) / 100;
  269.  
  270.    ewi.csr_x = _window[_window_spec.active].charo;
  271.  
  272.    fix_x_cursor();
  273.    fix_charo();
  274.    _window_refresh( _window_spec.active );
  275.    ed_put_cursor( _window_spec.active );
  276. }
  277.  
  278. void ems_up( void )
  279. {
  280.    /* move up by one char */
  281.    if( ewi.csr_y > 0 )
  282.       ewi.csr_y -= SMALL_SCROLL;
  283.  
  284.    fix_lineo();
  285.    fix_x_cursor();
  286.    _window_refresh( _window_spec.active );
  287.    ed_put_cursor( _window_spec.active );
  288.  
  289. }
  290.  
  291. void ems_down( void )
  292. {
  293.    /* move down by one char */
  294.    if( _window[_window_spec.active].blines > 0 )
  295.       if( ewi.csr_y < (_window[_window_spec.active].blines - 1) )
  296.          ewi.csr_y += SMALL_SCROLL;
  297.  
  298.    fix_lineo();
  299.    fix_x_cursor();
  300.    _window_refresh( _window_spec.active );
  301.    ed_put_cursor( _window_spec.active );
  302. }
  303.  
  304. void ems_pup( void )
  305. {
  306.    /* move up by one page */
  307.    unsigned pagesize = _window[_window_spec.active].usebottom -
  308.          _window[_window_spec.active].usetop + 1;
  309.  
  310.    if( ewi.csr_y >= pagesize   )
  311.       ewi.csr_y -= pagesize;
  312.    else
  313.       ewi.csr_y = 0;
  314.  
  315.    fix_lineo();
  316.    fix_x_cursor();
  317.    _window_refresh( _window_spec.active );
  318.    ed_put_cursor( _window_spec.active );
  319. }
  320.  
  321. void ems_pdown( void )
  322. {
  323.    /* move down by one page */
  324.    unsigned pagesize = _window[_window_spec.active].usebottom -
  325.          _window[_window_spec.active].usetop + 1;
  326.  
  327.    if( _window[_window_spec.active].blines == 0 )
  328.       return;
  329.  
  330.    ewi.csr_y += pagesize;
  331.  
  332.    while( ewi.csr_y >= _window[_window_spec.active].blines )
  333.       ewi.csr_y--;
  334.  
  335.    fix_lineo();
  336.    fix_x_cursor();
  337.    _window_refresh( _window_spec.active );
  338.    ed_put_cursor( _window_spec.active );
  339. }
  340.  
  341. void ems_vdrag( void )
  342. {
  343.    /* set line to percent of blines */
  344.  
  345.    if( _window[_window_spec.active].blines > 0 )
  346.       ewi.csr_y = _window_spec.scrinfo
  347.             * (_window[_window_spec.active].blines - 1 ) / 100;
  348.  
  349.    fix_lineo();
  350.    fix_x_cursor();
  351.    _window_refresh( _window_spec.active );
  352.    ed_put_cursor( _window_spec.active );
  353. }
  354.  
  355. /* keyboard functions */
  356.  
  357. /* put a character in */
  358. void _ed_char_ins( int *cxp, int *cyp, char the_char   )
  359. {
  360.    int count;
  361.    char *templine;
  362.  
  363.    templine = get_line( _window_spec.active, *cyp );
  364.  
  365.    /* impose character limit */
  366.    if( strlen( templine ) == CHARMAX )
  367.    {
  368.       printf("\a");
  369.       return;
  370.    }
  371.  
  372.    /* insert mode? */
  373.    if( (((_s_shift_key_read( ) & 0x0080 ) ? -1 : 0)) || (*cxp == strlen(templine) ) )
  374.    {
  375.       /* make space to insert into */
  376.         templine = _s_realloc( templine, (strlen( templine ) + 2) );
  377.       if( templine == NULL )
  378.             return;
  379.  
  380.       /* shift up to make space at cursor */
  381.       for( count = strlen(templine); count >= *cxp; count-- )
  382.          *(templine+count+1) = *(templine+count);
  383.    }
  384.  
  385.    /* insert at cursor, or overwrite */
  386.    *( templine + *cxp ) = the_char;
  387.  
  388.    /* save the text and move the cursor */
  389.    _wb_settext( _window_spec.active, templine );
  390.    (*cxp)++;
  391. }
  392.  
  393. void _ed_delete_fwd( int *cxp, int *cyp   )
  394. {
  395.    int count;
  396.    char *templine;
  397.  
  398.    templine = get_line( _window_spec.active, *cyp );
  399.  
  400.    if( *cxp == ( strlen(templine) ) )
  401.    {
  402.       templine = get_line( _window_spec.active, (*cyp + 1)   );
  403.  
  404.       if( templine == NULL )
  405.          return;
  406.  
  407.       (*cyp)++;
  408.       (*cxp) = 0;
  409.    }
  410.  
  411.    /* delete in the line */
  412.  
  413.    for( count = *cxp; count < strlen( templine ); count++ )
  414.       *(templine+count) = *(templine+1+count);
  415.  
  416.     templine = _s_realloc( templine, (strlen( templine ) + 1) );
  417.    if( templine == NULL )
  418.         return;
  419.  
  420.    _wb_settext( _window_spec.active, templine );
  421. }
  422.  
  423. void _ed_backspace( int *cxp, int *cyp   )
  424. {
  425.    int count;
  426.    char ec_store;
  427.    char *newline;
  428.    char *templine;
  429.  
  430.    templine = get_line( _window_spec.active, *cyp );
  431.  
  432.    if( *cxp != 0 )
  433.    {
  434.       /* delete in the line */
  435.  
  436.       for( count = (*cxp - 1); count < strlen( templine ); count++ )
  437.          *(templine+count) = *(templine+1+count);
  438.  
  439.         templine = _s_realloc( templine, (strlen( templine ) + 1) );
  440.       if( templine == NULL )
  441.             return;
  442.  
  443.       _wb_settext( _window_spec.active, templine );
  444.       (*cxp)--;
  445.    }
  446.    else
  447.    {
  448.       /* delete back to previous line */
  449.       /* if previous line is an endpara then combine two lines,
  450.          otherwise, move to previous line and delete last char
  451.       */
  452.  
  453.       ec_store = _wb_getend( _window_spec.active );
  454.       newline = get_line( _window_spec.active, (*cyp - 1) );
  455.  
  456.       if( _wb_getend( _window_spec.active ) == ENDPARA )
  457.       {
  458.          /* combine lines */
  459.  
  460.          *cxp = strlen( newline );
  461.  
  462.          /* need more space... */
  463.             newline = _s_realloc( newline,
  464.             ( strlen(newline) + strlen(templine) + 1 ) );
  465.          if( newline == NULL )
  466.                 return;
  467.  
  468.          newline = strcat( newline, templine );
  469.          _wb_settext( _window_spec.active, newline );
  470.          _wb_setend( _window_spec.active, ec_store );
  471.  
  472.          /* delete the old line */
  473.          _wb_gotoline( _window_spec.active, *cyp );
  474.          _wb_delline( _window_spec.active );
  475.  
  476.             (*cyp)--;
  477.  
  478.       }
  479.       else
  480.       {
  481.          (*cyp)--;
  482.  
  483.          templine = get_line( _window_spec.active, *cyp );
  484.          *(templine + strlen( templine ) - 1 ) = 0;
  485.             templine = _s_realloc( templine, (strlen( templine ) + 1) );
  486.             if( templine == NULL )
  487.                 return;
  488.  
  489.          _wb_settext( _window_spec.active, templine );
  490.          *cxp = strlen( newline );
  491.  
  492.         }
  493.    }
  494. }
  495.  
  496. void _ed_return( int *cxp, int *cyp   )
  497. {
  498.    char *newline;
  499.    char *templine;
  500.  
  501.    int count;
  502.    char ec_store;
  503.  
  504.    templine = get_line( _window_spec.active, *cyp );
  505.  
  506.    /* need to split line if not at the end */
  507.     if( *cxp == ( strlen(templine) /*+ 1*/ ) )
  508.    {
  509.       /* start an ordinary new line */
  510.       /* terminate the current line with an endpara */
  511.       _wb_setend( _window_spec.active, ENDPARA );
  512.  
  513.       /* newline, empty it, and move to it */
  514.       _wb_newline( _window_spec.active );
  515.         _wb_settext( _window_spec.active, _s_strdup("") );
  516.       _wb_setend( _window_spec.active, ENDPARA );
  517.  
  518.    }
  519.    else
  520.    {
  521.       /* get the end type which will now apply to the new line...*/
  522.       ec_store = _wb_getend( _window_spec.active );
  523.  
  524.       /* split this line into a new one. */
  525.       _wb_newline( _window_spec.active );
  526.  
  527.       /* allocate for the new line */
  528.         newline = _s_malloc( (size_t)(strlen(templine) - *cxp + 1 ) );
  529.       if( newline == NULL )
  530.             return;
  531.  
  532.       /* copy string into the new buffer */
  533.       for( count = 0; count < ( strlen(templine) - *cxp + 1 ); count++ )
  534.          *(newline + count) = *(templine + count + *cxp );
  535.  
  536.       *(templine + *cxp) = 0;
  537.  
  538.       /* reallocate the old buffer to size it down */
  539.         templine = _s_realloc( templine, (size_t)( *cxp + 1 ) );
  540.       if( templine == NULL )
  541.             return;
  542.  
  543.       /* store lines */
  544.       _wb_gotoline( _window_spec.active, *cyp );
  545.       _wb_settext( _window_spec.active, templine );
  546.       _wb_setend( _window_spec.active, ENDPARA );
  547.  
  548.       _wb_gotoline( _window_spec.active, *cyp + 1 );
  549.       _wb_settext( _window_spec.active, newline );
  550.       _wb_setend( _window_spec.active, ec_store );
  551.    }
  552.  
  553.    *cyp = * cyp + 1;
  554.     *cxp = 0;
  555. }
  556.  
  557. void _ed_home( int *cxp,  int *cyp )
  558. {
  559.    *cxp = 0;
  560. }
  561.  
  562. void _ed_end( int *cxp,   int *cyp )
  563. {
  564.    char *templine;
  565.    templine = get_line( _window_spec.active, *cyp );
  566.    *cxp = strlen( templine );
  567. }
  568.  
  569. void _ed_insert( void )
  570. {
  571.    _s_settextcursor( ((_s_shift_key_read( ) & 0x0080 ) ? -1 : 0)?
  572.          _steph_spec.cursor:_steph_spec.otcursor );
  573. }
  574.  
  575. void _ed_cursor_left( int *cxp, int *cyp   )
  576. {
  577.    *cxp = *cxp - 1;
  578.  
  579.    if(*cxp < 0)
  580.    {
  581.       if( *cyp == 0 )
  582.          *cxp = *cxp + 1;
  583.       else
  584.       {
  585.          *cyp = *cyp - 1;
  586.          _ed_end( cxp, cyp );
  587.       }
  588.    }
  589. }
  590.  
  591. void _ed_cursor_right( int *cxp, int *cyp   )
  592. {
  593.    char *templine;
  594.  
  595.    templine = get_line( _window_spec.active, *cyp );
  596.    *cxp = *cxp + 1;
  597.  
  598.    if( *cxp > strlen(templine))
  599.    {
  600.       if( *cyp == (_window[_window_spec.active].blines - 1) )
  601.          *cxp = strlen(templine);
  602.       else
  603.       {
  604.          *cxp = 0;
  605.          *cyp = *cyp + 1;
  606.       }
  607.    }
  608. }
  609.  
  610. void _ed_cursor_up( int *cxp, int *cyp )
  611. {
  612.    char *templine;
  613.  
  614.    if( *cyp > 0 )
  615.       *cyp = *cyp - 1;
  616.  
  617.    templine = get_line( _window_spec.active, *cyp );
  618.  
  619.    if( *cxp > strlen( templine ) )
  620.       *cxp = strlen( templine );
  621. }
  622.  
  623. void _ed_cursor_down( int *cxp, int *cyp )
  624. {
  625.    char *templine;
  626.  
  627.    if( *cyp < ( _window[_window_spec.active].blines - 1 ) )
  628.       *cyp = *cyp + 1;
  629.  
  630.    templine = get_line( _window_spec.active, *cyp );
  631.  
  632.    if( *cxp > strlen( templine ) )
  633.       *cxp = strlen( templine );
  634. }
  635.  
  636. void _ed_pg_up( int *cxp, int *cyp )
  637. {
  638.    unsigned pagesize = _window[_window_spec.active].usebottom -
  639.          _window[_window_spec.active].usetop + 1;
  640.  
  641.    char *templine;
  642.  
  643.    if( *cyp >= pagesize )
  644.       *cyp = *cyp - pagesize;
  645.    else
  646.       *cyp = 0;
  647.  
  648.    templine = get_line( _window_spec.active, *cyp );
  649.  
  650.    if( *cxp > strlen( templine ) )
  651.       *cxp = strlen( templine );
  652. }
  653.  
  654. void _ed_pg_down( int *cxp, int *cyp )
  655. {
  656.    unsigned pagesize = _window[_window_spec.active].usebottom -
  657.          _window[_window_spec.active].usetop + 1;
  658.  
  659.    char *templine;
  660.  
  661.    if( _window[_window_spec.active].blines == 0 )
  662.       return;
  663.  
  664.    if( (*cyp+pagesize) <= ( _window[_window_spec.active].blines - 1 ) )
  665.       *cyp = *cyp + pagesize;
  666.    else
  667.       *cyp = _window[_window_spec.active].blines - 1;
  668.  
  669.    templine = get_line( _window_spec.active, *cyp );
  670.  
  671.    if( *cxp > strlen( templine ) )
  672.       *cxp = strlen( templine );
  673. }
  674.  
  675.  
  676. /* the editor action function */
  677.  
  678. void editor_action( void )
  679. {
  680.    char redraw = FALSE;    /* do we need to redraw window? TRUE = YES */
  681.  
  682.    int cx, cy;             /* working cursor position variables */
  683.  
  684.    unsigned char key1= _window_spec.key1;
  685.    unsigned char key2= _window_spec.key2;
  686.  
  687.    unsigned winsize = _window[_window_spec.active].usebottom -
  688.          _window[_window_spec.active].usetop + 1;
  689.  
  690.    cy = ewi.csr_y;
  691.    cx = ewi.csr_x;
  692.  
  693.    /* there are sometimes big keyboard repeats, eg in delete, so soak up
  694.       excess keystrokes
  695.    */
  696.    _s_flush_keyboard();
  697.  
  698.  
  699.    /* if the window has no buffer lines, then create the zeroth one */
  700.    if( _window[_window_spec.active].blines == 0 )
  701.    {
  702.       _wb_newline( _window_spec.active );
  703.  
  704.       /* all wb texts must be dynamic... */
  705.         _wb_settext( _window_spec.active, _s_strdup( "" ) );
  706.  
  707.       /* it must be the end of the document... */
  708.       _wb_setend( _window_spec.active, ENDDOC );
  709.    }
  710.  
  711.    /* insert the character and advance the cursor */
  712.    if( ( key1 >= 32 ) && ( key1 <= 126 ) )
  713.    {
  714.       _ed_char_ins( &cx, &cy, key1 );
  715.       redraw = TRUE;
  716.    }
  717.    else
  718.  
  719.    /* backspace */
  720.    if( ( key1 == 8 ) && ( (cy > 0) || (cx > 0) ) )
  721.    {
  722.       _ed_backspace( &cx, &cy );
  723.       redraw = TRUE;
  724.    }
  725.    else
  726.  
  727.    /* delete */
  728.    if( key2 == 83 )
  729.    {
  730.       _ed_delete_fwd( &cx, &cy );
  731.       redraw = TRUE;
  732.    }
  733.    else
  734.  
  735.    /* insert (overtype toggle) */
  736.    if( key2 == 82 )
  737.    {
  738.       _ed_insert();
  739.    }
  740.    else
  741.  
  742.    /* return key pressed */
  743.    if( key1 == 13 )
  744.    {
  745.       _ed_return( &cx, &cy );
  746.       redraw = TRUE;
  747.    }
  748.    else
  749.  
  750.    /* cursor left */
  751.    if( key2 == 75 )
  752.    {
  753.       _ed_cursor_left( &cx, &cy );
  754.    }
  755.    else
  756.  
  757.    /* cursor right */
  758.    if( key2 == 77 )
  759.    {
  760.       _ed_cursor_right( &cx, &cy   );
  761.    }
  762.    else
  763.  
  764.    /* cursor up */
  765.    if( key2 == 72 )
  766.    {
  767.       _ed_cursor_up( &cx, &cy );
  768.    }
  769.    else
  770.  
  771.    /* cursor down */
  772.    if( key2 == 80 )
  773.    {
  774.       _ed_cursor_down( &cx, &cy );
  775.    }
  776.    else
  777.  
  778.    /* home */
  779.    if( key2 == 71 )
  780.    {
  781.       _ed_home( &cx, &cy );
  782.    }
  783.    else
  784.  
  785.    /* end */
  786.    if( key2 == 79 )
  787.    {
  788.       _ed_end( &cx, &cy );
  789.    }
  790.    else
  791.  
  792.    /* pgup */
  793.    if( key2 == 73 )
  794.    {
  795.       _ed_pg_up( &cx, &cy );
  796.    }
  797.    else
  798.  
  799.    /* pgdn */
  800.    if( key2 == 81 )
  801.    {
  802.       _ed_pg_down( &cx, &cy );
  803.    }
  804.  
  805.  
  806.    /* check that cursor is in screen and scroll otherwise */
  807.  
  808.    while( cx < _window[_window_spec.active].charo)
  809.    {
  810.       _window[_window_spec.active].charo -= SMALL_SCROLL;
  811.       redraw = TRUE;
  812.    }
  813.  
  814.    while( cx > (_window[_window_spec.active].charo + 77) )
  815.    {
  816.       _window[_window_spec.active].charo += SMALL_SCROLL;
  817.       redraw = TRUE;
  818.    }
  819.  
  820.    while( cy < _window[_window_spec.active].lineo)
  821.    {
  822.       _window[_window_spec.active].lineo -= SMALL_SCROLL;
  823.       redraw = TRUE;
  824.    }
  825.  
  826.    while( cy >= (_window[_window_spec.active].lineo + winsize ) )
  827.    {
  828.       _window[_window_spec.active].lineo += SMALL_SCROLL;
  829.       redraw = TRUE;
  830.    }
  831.  
  832.  
  833.    /* refresh */
  834.    if( redraw )
  835.       _window_refresh( _window_spec.active );
  836.  
  837.    ewi.csr_x = cx;
  838.    ewi.csr_y = cy;
  839.  
  840.     ed_put_cursor( _window_spec.active );
  841.  
  842.  
  843.  
  844. }
  845.  
  846.  
  847.  
  848.  
  849.  
  850. /**********************************************************************/
  851.  
  852. /* function to turn an editor window buffer set into
  853.     a buffer with \n to delimit lines */
  854.  
  855. char *_ed_makebuffer( unsigned win )
  856. {
  857.     /* count through lines, adding each to the buffer */
  858.  
  859.     char *buffer;
  860.     char *thisline;
  861.  
  862.     unsigned slen = 0;
  863.     unsigned savelen = 0;
  864.  
  865.     unsigned linenum = 0;
  866.     unsigned count;
  867.  
  868.     buffer = _s_malloc( 0 );
  869.     if( buffer == NULL )
  870.         return NULL;
  871.  
  872.  
  873.     do
  874.     {
  875.         _wbcc_uncolour( win, linenum );
  876.         thisline = get_line( win, linenum );
  877.  
  878.         slen += 1 + strlen( thisline );
  879.  
  880.         /* reallocate the buffer */
  881.  
  882.         buffer = _s_realloc( buffer, slen );
  883.         if( buffer == NULL )
  884.             return NULL;
  885.  
  886.         /* copy the string into the buffer */
  887.  
  888.         for( count = savelen; count < (slen    - 1); count++ )
  889.             *(buffer+count) = *(thisline+count-savelen);
  890.         *(buffer+slen-1) = '\n';
  891.  
  892.         savelen = slen;
  893.  
  894.         linenum += 1;
  895.     }while( _window[win].pwb->link != NULL );
  896.  
  897.     /* done all lines */
  898.  
  899.     *(buffer+savelen) = '\0';
  900.  
  901.     return buffer;
  902. }
  903.  
  904. /******************************************************/
  905.  
  906.  
  907. int _ed_readfile( unsigned win, char *fname )
  908. {
  909.     /* load a file from disk */
  910.  
  911.     FILE *infile;
  912.     unsigned line;
  913.     char tmp[CHARMAX+1];
  914.     unsigned readlen;
  915.  
  916.     /* open the file */
  917.  
  918.     infile = fopen( fname, "rt" );
  919.  
  920.     if( infile == NULL ) return FALSE;
  921.  
  922.     /* file opened ok */
  923.  
  924.     /* empty the window buffer */
  925.  
  926.     while( _window[win].blines > 0 )
  927.         _wb_delline( win );
  928.  
  929.     while(1)
  930.     {
  931.         /* read a line until end or error */
  932.  
  933.         if( fgets( tmp, CHARMAX, infile ) == NULL )
  934.             break;
  935.             /* might be EOF or error */
  936.  
  937.         /* store in window buffer after removing the newline */
  938.  
  939.         readlen = strlen(tmp);
  940.  
  941.         if( *(tmp+readlen-1) == 0x0a )
  942.             *(tmp+readlen-1) = '\0';
  943.  
  944.         _wb_newline( win );
  945.         _wb_settext( win, _s_strdup(tmp) );
  946.     }
  947.  
  948.     /* done */
  949.  
  950.     fclose( infile );
  951.  
  952.     return TRUE;
  953. }
  954.